package employeeTask;

public class Employee {
	
	private static final String DEFAULT_NAME = "Huan Amigo Maria Rodgrigez";
	private static final int MAX_WORKING_HOURS = 8;
	
	private String name;
	private Task currentTask;
	private int hoursLeft;
	private static AllWork allwork;
	
	
	public Employee(String name) {
		if ( name != null && !name.equals("")) {
			this.name = name;
		}
		else {
			this.name = DEFAULT_NAME;
		}
		
		hoursLeft = MAX_WORKING_HOURS;
	}
	
	/**
	 * @return the currentTask
	 */
	public Task getCurrentTask() {
		return currentTask;
	}
	
	/**
	 * @param currentTask the currentTask to set
	 */
	public void setCurrentTask(Task currentTask) {
		if (currentTask != null)
			this.currentTask = currentTask;
	}
	
	
	/**
	 * @return the hoursLeft
	 */
	public int getHoursLeft() {
		return hoursLeft;
	}
	
	/**
	 * @param hoursLeft the hoursLeft to set
	 */
	public void setHoursLeft(int hoursLeft) {
		if ( hoursLeft >= 0 && hoursLeft <= MAX_WORKING_HOURS )
			this.hoursLeft = hoursLeft;
	}
	
	/**
	 * @return the allwork
	 */
	public static AllWork getAllwork() {
		return allwork;
	}
	
	/**
	 * @param allwork the allwork to set
	 */
	public static void setAllWork(AllWork allwork) {
		if (allwork != null)
			Employee.allwork = allwork;
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	void work(Employee[] employees, int i) {
		if (allwork.isAllWorkDone()) return;
		if (getHoursLeft() > 0) {
			System.out.println("Employee named " + this.getName() + " starts working." );
			if (currentTask == null || currentTask.getWorkingHours() == 0) {
				currentTask = allwork.getNextTask();
				if ( currentTask != null )
					System.out.println("Employee named " + this.getName() + " started working on " +
						"task named " + currentTask.getName() );
				else
					System.out.println("No tasks! Sorry! ");
			}
			if ( currentTask == null )
				hoursLeft = 0;
			
			if (currentTask != null) {
				int hoursToWorkOnThisTask = Math.min(currentTask.getWorkingHours(), hoursLeft);
				setHoursLeft(getHoursLeft() - hoursToWorkOnThisTask);				
				currentTask.setWorkingHours(currentTask.getWorkingHours() - 
						hoursToWorkOnThisTask);
				System.out.println("Employee named " + this.getName() + " worked on " +
						"task named " + currentTask.getName() + " " + hoursToWorkOnThisTask + 
						" hours" );
			}
		}
		if (!allwork.isAllWorkDone()) {
			i++;
			if (i>=employees.length) i=0;
			employees[i].work(employees, i);
		}
 	}
	
	void startWorkingDay() {
		hoursLeft = MAX_WORKING_HOURS;
	}
	

}
